package com.example.Android_test.util;

import android.util.Log;
import com.example.Android_test.R;
import com.example.Android_test.activity.Main;
import com.example.Android_test.specs.LanguageSpec;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: anpi0413
 * Date: 30.05.13
 * Time: 13:20
 * To change this template use File | Settings | File Templates.
 */
public class LanguageUtils {
    //private LanguageSpec currentLanguage;
    //private static ArrayList<LanguageSpec> availableLanguages = new ArrayList<LanguageSpec>();

    public static ArrayList<LanguageSpec> getAvailableLanguages() {
        ArrayList<LanguageSpec> availableLanguages = new ArrayList<LanguageSpec>();

        String langItems[] = Main.getAppContext().getResources().getStringArray(R.array.array_available_languages);
        for (String langLine: langItems) {
            Log.d(Main.LOG_TAG, "read lang line: " + langLine);
            String cols[] = langLine.split("\\|");
            Log.d(Main.LOG_TAG, "read lang line: " + cols[0] + "|" + cols[1]);
            availableLanguages.add(new LanguageSpec(cols[0], cols[1]));
        }

        return availableLanguages;
    }

    public static LanguageSpec getCurrentLanguage() {
        ArrayList<LanguageSpec> availableLanguages = getAvailableLanguages();

        String locale = Main.getAppContext().getResources().getConfiguration().locale.getLanguage();

        for (LanguageSpec lang: availableLanguages) {
            Log.d(Main.LOG_TAG, "read lang line: " + lang);
            if (lang.getCode().equalsIgnoreCase(locale)) return lang;
        }

        return null;
    }
}
